
// canvas 图片验证码
let nums = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
];
let str = '';

// 绘制验证码
export function drawCode() {
    let canvas: any = document.getElementById("verifyCanvas"); //获取HTML端画布
    let context = canvas.getContext("2d"); //获取画布2D上下文
    context.fillStyle = "cornflowerblue"; //画布填充色
    context.fillRect(0, 0, canvas.width, canvas.height); //清空画布
    context.fillStyle = "white"; //设置字体颜色
    context.font = "25px Arial"; //设置字体
    let rand: any = [];
    let x: any = [];
    let y: any = [];
    for (let i = 0; i < 4; i++) {
        rand.push(rand[i]);
        rand[i] = nums[Math.floor(Math.random() * nums.length)]
        x[i] = i * 20 + 10;
        y[i] = Math.random() * 20 + 20;
        context.fillText(rand[i], x[i], y[i]);
    }
    str = rand.join('').toUpperCase();
    //画5条随机线
    for (let i = 0; i < 5; i++) {
        drawline(canvas, context);
    }

    // 画30个随机点
    for (let i = 0; i < 30; i++) {
        drawDot(canvas, context);
    }
    convertCanvasToImage(canvas);
    return str;
}

// 随机线
function drawline(canvas: any, context: any) {
    context.moveTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height)); //随机线的起点x坐标是画布x坐标0位置，y坐标是画布高度的随机数
    context.lineTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height)); //随机线的终点x坐标是画布宽度，y坐标是画布高度的随机数
    context.lineWidth = 0.5; //随机线宽
    context.strokeStyle = 'rgba(50,50,50,0.3)'; //随机线描边属性
    context.stroke(); //描边，即起点描到终点
}
// 随机点(所谓画点其实就是画1px像素的线，方法不再赘述 同上)
function drawDot(canvas: any, context: any) {
    let px = Math.floor(Math.random() * canvas.width);
    let py = Math.floor(Math.random() * canvas.height);
    context.moveTo(px, py);
    context.lineTo(px + 1, py + 1);
    context.lineWidth = 0.2;
    context.stroke();

}
// 绘制图片
function convertCanvasToImage(canvas: any) {
    let verifyCanvas: any = document.getElementById("verifyCanvas");
    verifyCanvas.style.display = "none"
    let image: any = document.getElementById("code_img");
    image.src = canvas.toDataURL("image/png");
    return image;
}

// 点击图片刷新
export function resetCode() {
    let canvas: any = document.getElementById('verifyCanvas');
    if (canvas != null) {
        canvas.parentNode.removeChild(canvas);
    }

    let code_img: any = document.getElementById('code_img');
    let newCanvas = document.createElement("canvas");
    newCanvas.id = "verifyCanvas";
    newCanvas.width = 100;
    newCanvas.height = 40;
    code_img.parentElement.insertBefore(newCanvas, code_img);
    return drawCode();
}


// 去除所有标签
export function getInnerText(content: string) {
    let reg = /<\/?.+?\/?>/g;
    return content.replace(reg, '');
}

// localStorage
export function saveLocalStorage(key: string, value: any) {
    localStorage.setItem(key, JSON.stringify(value));
}

export function getLocalStorage(key: string) {
    let v: any = localStorage.getItem(key);
    return JSON.parse(v)
}

export function deleteLocalStorage(key: string) {
    localStorage.removeItem(key);
}